package{
	import com.PQLoad.PQLoader;
	import com.pqmvc.PQData;
	import com.tools.PQDrawTools;
	import com.tools.Tip;
	import com.tools.UIHelper;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;

	[SWF (width="1060",height="800")]
	public class MapConfigEditor extends Sprite{
		private var btn_last:Sprite;
		private var btn_next:Sprite;
		private var btn_go:Sprite;
		private var btn_grid:Sprite;
		
		private var txt:TextField;
		private var txt_page:TextField;
		
		private var panel:Sprite;
		private var index:int=1;
		private var grid:Sprite;
		public function MapConfigEditor(){
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		private function init(e:Event):void{
			panel=new Sprite();
			addChild(panel);
			panel.x=50;
			panel.y=50;
			panel.graphics.beginFill(0xffffff,0.1);
			panel.graphics.drawRect(-50,-50,1060,700);
			
			
			grid=new Sprite();
			addChild(grid);
			grid.visible=false;
			
			txt=UIHelper.self.createMultilineTxt({width:800,height:50,x:100,y:650,size:20,parent:this});
			
			var btnbg:Sprite=PQDrawTools.drawRoundRect(0,0,100,30,1);
			var btntxt:TextField=UIHelper.self.createLabel({text:"上一页",size:20,color:0x000,blod:true,x:18,y:3});
			btn_last=new Sprite();
			btn_last.addChild(btnbg);
			btn_last.addChild(btntxt);
			addChild(btn_last);
			
			btnbg=PQDrawTools.drawRoundRect(0,0,100,30,1);
			btntxt=UIHelper.self.createLabel({text:"下一页",size:20,color:0x000,blod:true,x:18,y:3});
			btn_next=new Sprite();
			btn_next.addChild(btnbg);
			btn_next.addChild(btntxt);
			addChild(btn_next);
			
			btn_next.x=960;
			btn_next.y=770;
			btn_last.x=0;
			btn_last.y=770;
			
			btn_last.buttonMode=true;
			btn_next.buttonMode=true;
			
			btn_last.addEventListener(MouseEvent.CLICK,lastPage);
			btn_next.addEventListener(MouseEvent.CLICK,nextPage);
			
			txt_page=UIHelper.self.createMultilineTxt({width:50,height:25,x:500,y:770,border:true,size:20,parent:this,mouseEnabled:true,type:TextFieldType.INPUT});
			
			btnbg=PQDrawTools.drawCircle(10,10,13,1,0x000000,0xffffff);
			btntxt=UIHelper.self.createLabel({text:"go",size:20,color:0x000,blod:true,x:0,y:-5,mouseEnabled:false,width:30,height:30});
			btn_go=new Sprite();
			btn_go.addChild(btnbg);
			btn_go.addChild(btntxt);
			addChild(btn_go);
			btn_go.x=560;
			btn_go.y=773;
			btn_go.buttonMode=true;
			btn_go.addEventListener(MouseEvent.CLICK,goPage);
			
			btnbg=PQDrawTools.drawRoundRect(0,0,50,25,1,0x000000,0xffffff);
			btntxt=UIHelper.self.createLabel({text:"网格",size:16,color:0x000,blod:true,x:5,y:2,mouseEnabled:false,width:50,height:30});
			btn_grid=new Sprite();
			btn_grid.addChild(btnbg);
			btn_grid.addChild(btntxt);
			addChild(btn_grid);
			btn_grid.x=800;
			btn_grid.y=770;
			btn_grid.buttonMode=true;
			btn_grid.addEventListener(MouseEvent.CLICK,setGrid);
			
			panel.addEventListener(MouseEvent.CLICK,setPoint);
			
			loadRes();
			showGrid();
		}
		private function loadRes():void{
			txt.text="第"+index+"关:   ";
			PQLoader.load("swf/scene/Map"+index+".swf",{onComplete:showMap,onError:loadError});
		}
		private function showMap(obj:Object):void{
			panel.removeChildren();
			panel.addChild(obj as DisplayObject);
		}
		private function loadError(e:*):void{
			Tip.showTip("第"+index+"关场景资源不存在",this.stage);
			panel.removeChildren();
		}
		private function lastPage(e:MouseEvent):void{
			if(index>1){
				index--;
				btn_next.visible=true;
			}
			else btn_last.visible=false;
			loadRes();
		}
		private function nextPage(e:MouseEvent):void{
			if(index<10000){
				index++;
				btn_last.visible=true;
			}
			else btn_next.visible=false;
			loadRes();
		}
		private function goPage(e:MouseEvent):void{
			index=int(txt_page.text);
			loadRes();
		}
		private function setPoint(e:MouseEvent):void{
			txt.text+=e.localX+","+e.localY+"|";
			var sp:Sprite=PQDrawTools.drawCircle(0,0,5,5,0x000,0xff0000);
			panel.addChild(sp);
			sp.x=e.localX;
			sp.y=e.localY;
		}
		private function setGrid(e:MouseEvent):void{
			grid.visible=!grid.visible;
		}
		private function showGrid():void{
			var space:int=20;
			var c:int=this.stage.stageWidth/space;
			var r:int=this.stage.stageHeight/space;
			grid.graphics.lineStyle(1,0x999999);
			for(var i:int=1;i<r;i++){
				grid.graphics.moveTo(0,i*space);
				grid.graphics.lineTo(1060,i*space);
			}
			for(var j:int=1;j<c;j++){
				grid.graphics.moveTo(j*space,0);
				grid.graphics.lineTo(j*space,800);
			}
		}
	}
}